package ModelLayer;
import java.util.ArrayList;

/**
 * The CustomerCon class is a container class, where objects of
 * the type Customer can be added and found in a list.
 * The class is incorporated as Singleton.
 * 
 * @author  Jakob K., Mie P., Anna K., Anders A.P., Danny M.P.  
 * @version 1.0
 */
public class CustomerCon {

    private ArrayList<Customer> customers;
    private static CustomerCon  instance;

    /**
     * Constructor for objects of class CustomerCon.
     */
    private CustomerCon() {

        customers = new ArrayList<Customer>();

    }

    /**
     * Method to get an instance of the Customer object.
     * @return a single CustomerCon object.
     */
    public static CustomerCon getInstance() {

        if(instance == null) {
            instance = new CustomerCon();

        }
        return instance;

    }

    /**
     * Method that adds a Customer object to the list.
     * 
     *@param object of type Customer.
     *@throws Exception if the Customer object already is added to the collection.
     */
    public void addCustomer(Customer c) throws Exception {

        if (!customers.contains(c)) {
            customers.add(c);
        }
        else {
            throw new Exception ("Kunden er allerede tilføjet.");
        }

    }

    /**
     * Method that gets a customer
     * 
     * @return object of type customer.
     * 
     * @param takes a index number.
     */
    public Customer getCustomer(int customerNo){

        Customer c = null;
        boolean found = false;
        for (int index = 0; index < customers.size() && !found; index++) {
            if (customers.get(index).getCustomerNo() == customerNo) {
                c = customers.get(index);
                found = true;
            }
        }
        if (c != null) {
            return c;
        }
        else {
            throw new IllegalArgumentException("Kunden er ikke fundet.");
        }

    }

    /**
     * A method to remove a Customer object from the collection found by its id-number.
     * 
     * @param customerNo The id-number of the customer.
     */
    public void deleteCustomer(int customerNo) {

        customers.remove(getCustomer(customerNo));

    }

}